package com.example.demospring.controller;

import lombok.Data;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

@RestController
@RequestMapping("/hello")
public class SumUpController {

    //get请求获取数据
    @GetMapping("/get1")
    public String get1(){
        return "这是你拿到的数据";
    }
    //使用@RequestParam传递多个参数
    @GetMapping("/get2")
    public String get2(@RequestParam int a, @RequestParam int b){
        return "你传的两个数是" + a + " " + b;
    }

    //路径参数GET请求示例
    @GetMapping("/get3/{pn}/{ps}")
    public String get3(@PathVariable("ps") int ps, @PathVariable("pn") int pn){
        return "你的查找要求是一页" + ps + "条数据的第" +  pn + "页";
    }

    @Data
    public static class ResponseData {
        private String status;
        private String message;
    }
    // 返回JSON数据
    @GetMapping("/get4")
    public ResponseData get4() {
        ResponseData responseData = new ResponseData();
        responseData.setStatus("success");
        responseData.setMessage("这是一条成功的消息。");
        return responseData;
    }

    @Data
    public static class Person {
        private String name;
        private int age;
    }
    //使用对象作为请求参数
    @PostMapping("/post1")
    public String post1(@RequestBody Person person) {
        return "你传的姓名: " + person.getName() + " 年龄: " + person.getAge() + "。";
    }


    // 使用Map接收JSON数据的POST请求示例
    @PostMapping("/post2")
    public String post2(@RequestBody Map<String, String> mp) {
        AtomicReference<String> data = new AtomicReference<>("");
        mp.forEach((k, v) ->{
            data.set(data + k);
            data.set(data + ": ");
            data.set(data + v + ",");
        });
        return "你传的键值对是: " + data;
    }


    @PutMapping("/putExample")
    public String putExample(@RequestBody String data) {
        return "这是PUT请求，传入的数据是: " + data;
    }

    @DeleteMapping("/deleteExample/{id}")
    public String deleteExample(@PathVariable("id") int id) {
        return "这是DELETE请求，要删除的数据ID是: " + id;
    }


}
